import tkinter as tk
from PIL import Image, ImageTk
from utils.image import load_and_resize_image

# import tkapp_left
# import tkapp_bottom
from tkapp_left import AppLeft
from tkapp_bottom_o import AppBottom
import db.sqlite3 as db
import os
from pathlib import Path

base_path = os.path.dirname(os.path.abspath(__file__))
 
# 预设竖屏图片尺寸
I_WIDTH = 800
I_HEIGHT = 600

# print(f"tk.TkVersion:{tk.TkVersion}")

# 窗口居中方法
def center_window(window, width, height):
    # 获取屏幕宽度和高度
    screen_width = window.winfo_screenwidth()
    screen_height = window.winfo_screenheight()
 
    # 计算窗口的x和y坐标，使窗口居中
    x = (screen_width - width) // 2
    y = (screen_height - height) // 2
 
    # 设置窗口的大小和位置
    window.geometry(f'{width}x{height}+{x}+{y}')

def update_background(event):
    """在窗口调整大小时更新背景图片"""
    # global background_photo
    # new_width = root.winfo_width()
    # new_height = root.winfo_height()
 
    # 强制更新界面
    # root.update_idletasks()

    # 更新窗口以显示图片
    # root.mainloop()
    pass

def on_label_click(event, link):
    # print(event)
    tkapp_right.update(link)

# 创建主窗口
root = tk.Tk()

# 隐藏窗口
root.withdraw()

root.title("LI自动部署")
icon_path = os.path.join(base_path, Path('static') / 'img' / 'icon.ico')
root.wm_iconbitmap(icon_path)# 使用wm_iconbitmap引入创建的ico
 
# 设置窗口初始大小
window_width = round(1920 // 1.6)
window_height = round(1080 // 1.6)
root.geometry(f'{window_width}x{window_height}')

center_window(root, window_width, window_height)

# 设置最小宽度为300像素，高度不限定
root.minsize(width=window_width, height=window_height)  # 这里的100是初始高度，可以根据需要调整
# root.resizable(width=False, height=True)  # 宽度不可调整，高度可调整

# 禁止改变窗口大小
# root.resizable(width=False, height=False)
 
# 加载并调整图片尺寸
# 拼接配置文件路径
image_path = os.path.join(base_path, Path('static') / 'img' / 'a.jpg')
# image_path = "static/img/a.jpg"
# background_photo = load_and_resize_image(image_path, root.winfo_width(), root.winfo_height())
background_photo = load_and_resize_image(image_path, window_width, 100)

# 创建一个Label，用于显示图片
background_label = tk.Label(root, image = background_photo)
# background_label.pack()
# background_label.place()
 
# 绑定窗口大小变化事件
root.bind('<Configure>', update_background)

# 创建一个垂直的 PanedWindow
vertical_paned_window = tk.PanedWindow(root, orient=tk.VERTICAL)
vertical_paned_window.pack(fill=tk.BOTH, expand=True)

# 创建一个 PanedWindow，设置为水平分隔
horizontal_paned_window = tk.PanedWindow(vertical_paned_window, orient=tk.HORIZONTAL, height=350)
# horizontal_paned_window.pack(fill=tk.BOTH, expand=True)
# 嵌套一个水平的 PanedWindow 到垂直的 PanedWindow 中
vertical_paned_window.add(horizontal_paned_window, minsize=250, stretch="first")
 
# 添加左列表
tkapp_left = AppLeft()
left = tkapp_left.create(horizontal_paned_window, on_label_click)
horizontal_paned_window.add(left, minsize=150)

# 添加右侧容器
# import tkapp_right
from tkapp_right_o import AppRight
tkapp_right = AppRight()
right = tkapp_right.create(horizontal_paned_window)
horizontal_paned_window.add(right)

# 添加第三个 Label 到垂直的 PanedWindow
tkapp_bottom = AppBottom()
bottom = tkapp_bottom.create(vertical_paned_window, None)
vertical_paned_window.add(bottom, minsize=100, height=190, stretch="never")

tkapp_right.useBottom(tkapp_bottom)
tkapp_right.useLeft(tkapp_left)
tkapp_left.useRigth(tkapp_right)

# 显示窗口
root.deiconify()
# 进入主事件循环
root.mainloop()

